package com.swim.pay.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.jeesite.common.lang.StringUtils;
import com.swim.common.utils.ConcurrentHashMapCacheUtils;
import com.swim.common.utils.ConstantUtil;
import com.swim.common.utils.MD5Utils;
import com.swim.common.utils.SmsUtil;
import com.swim.common.utils.WXPayUtil;
import com.swim.common.utils.XMLUtils;
import com.swim.natatoriumgoods.entity.NatatoriumGoods;
import com.swim.natatoriumgoods.service.NatatoriumGoodsService;
import com.swim.natatoriumorder.entity.NatatoriumOrder;
import com.swim.natatoriumorder.service.NatatoriumOrderService;
import com.swim.pay.bean.AliPayBean;
import com.swim.pay.service.PayService;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_PATTERN;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author tql
 * @Description 支付控制器类(游泳商品)
 * @Date 21-2-27 下午8:35
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "${adminPath}/pay")
public class SwimPayController {

	private Logger logger = LoggerFactory.getLogger(SwimPayController.class);


	@Autowired
	private PayService payService;

	@Autowired
	private NatatoriumOrderService natatoriumOrderService;

	@Autowired
	private NatatoriumGoodsService natatoriumGoodsService;

	//#####################################################################
	//############################# 微信PC支付 #############################
	//#####################################################################

	/**
	 * 微信支付url（PC端）
	 *
	 * @param totalFee
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/get/vx/pay/url")
	@ResponseBody
	public Object weixinPcPayByQRCode(@RequestParam String totalFee, @RequestParam String outTradeNo) throws Exception {
		NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
		natatoriumOrder.setOrderNum(outTradeNo);
		List<NatatoriumOrder> natatoriumOrder1 = natatoriumOrderService.findList(natatoriumOrder);
		HashMap<String, String> stringStringHashMap = new HashMap<>();
		if (natatoriumOrder1.size() > 0) {
			//应付金额
			Double shouldPay = natatoriumOrder1.get(0).getShouldPay();

			//判断金额是否和消费记录中一致
			if (StringUtils.isBlank(totalFee) || Double.parseDouble(totalFee) != shouldPay) {
				stringStringHashMap.put("status", "error");
				stringStringHashMap.put("message", "付款金额和订单订单金额不一致！");
				return stringStringHashMap;
			}
			Map<String, String> resultMap = payService.getWeixinPayCodeUrl(outTradeNo, totalFee, "NATIVE");
			return resultMap;
		}
		stringStringHashMap.put("status", "error");
		return stringStringHashMap;
	}

	/**
	 * 查询微信支付状态
	 *
	 * @param outTradeNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/query/vx/pay/status")
	@ResponseBody
	public Object queryWeixinPayStatus(@RequestParam String outTradeNo) throws Exception {
		Map<String, String> resultMap = new HashMap<String, String>();
		while (true) {
			Map<String, String> map = payService.queryWeixinPayStatus(outTradeNo);
			if (map == null) {
				resultMap.put("status", "error");
				break;
			}
			if ("SUCCESS".equals(map.get("trade_state"))) {
				resultMap.put("status", "success");
				//缓存存在说明该订单已经付款成功，并且多次调用本接口了，所以直接返回，防止多次走下面的代码
				if(ConcurrentHashMapCacheUtils.getCache(outTradeNo) != null) {
					return resultMap;
				}
				NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
				natatoriumOrder.setOrderNum(outTradeNo);
				List<NatatoriumOrder> natatoriumOrder1 = natatoriumOrderService.findList(natatoriumOrder);
				orderPaySuccess(natatoriumOrder1.get(0));
				//设置缓存，24小时后过期
				ConcurrentHashMapCacheUtils.setCache(outTradeNo, outTradeNo, 1440000);
				break;
			}
			Thread.sleep(3000);
		}
//        natatoriumOrderService.updateOrderStatus(outTradeNo, 2L);
		return resultMap;
	}

	/**
	 * 更新订单支付状态(订单支付成功)
	 *
	 * @param natatoriumOrder
	 * @return id
	 */
	public void orderPaySuccess(NatatoriumOrder natatoriumOrder) {
		//防止前端重复回调，从而发送多条短信
		if(natatoriumOrder.getOrderStatus() != 2) {// 订单状态 1表示付款中 2表示已付款 3表示已核销 4表示已关闭
			//付款成功后给用户发送核销验证码
			String code = "";
			try {
				Random random = new Random();
				for (int i = 0; i < 6; i++) {
					code += random.nextInt(10);
				}
				// 发送验证码
				SmsUtil.sendSettlementCode(natatoriumOrder.getOrderNum(), natatoriumOrder.getPayerPhone(), code);
			} catch (Exception e) {
				logger.error("发送核销码短信异常");

				System.out.println("发送核销码短信异常");
			}
			//把核销验证码保存到订单中
			natatoriumOrder.setSettlementCode(code);
			//支付时间
			natatoriumOrder.setPayDate(new Date());
			//修改订单状态
			natatoriumOrder.setOrderStatus(2L);// 订单状态 1表示付款中 2表示已付款 3表示已核销 4表示已关闭
			if (natatoriumOrder.getNatatoriumGoods().getGoodsType() == 1L) {// 商品类型 1表示游泳票 2表示培训课程
				natatoriumOrder.setSettlementStatus(1L);        // 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
			} else if (natatoriumOrder.getNatatoriumGoods().getGoodsType() == 2L) {
				natatoriumOrder.setSettlementStatus(3L);
			}
			natatoriumOrderService.update(natatoriumOrder);

			//更新商品库存
			NatatoriumGoods natatoriumGoods = natatoriumGoodsService.get(natatoriumOrder.getNatatoriumGoodId());
			natatoriumGoods.setNumber(natatoriumGoods.getNumber() - 1);
			natatoriumGoodsService.update(natatoriumGoods);
		}
	}

	//#####################################################################
	//############################# 微信H5支付 #############################
	//#####################################################################

	@ResponseBody
	@RequestMapping(value = "/get/vx/h5/pay/url" ,produces = { "application/json;charset=UTF-8" })
	public String weixinPayWap(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String subject = request.getParameter("subject");//前端上送的支付主题
		String total_amount = request.getParameter("totalAmount");//前端上送的支付金额
		String out_trade_no = request.getParameter("outTradeNo");//前端上送的订单号

		//判断支付金额是否和消费记录中一致
		JSONObject result = new JSONObject();
		NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
		natatoriumOrder.setOrderNum(out_trade_no);
		List<NatatoriumOrder> natatoriumOrder1 = natatoriumOrderService.findList(natatoriumOrder);
		if (natatoriumOrder1.size() > 0) {
			//应付金额
			Double shouldPay = natatoriumOrder1.get(0).getShouldPay();
			if (StringUtils.isBlank(total_amount) || Double.parseDouble(total_amount) != shouldPay) {
				result.put("status", "error");
				result.put("message", "付款金额和订单订单金额不一致！");
				return result.toString();
			}
		}

//		String APPID = "wx23d5211634ee5234";//公众号的appid
//        String MERID = ConstantUtil.PARTNER_ID;//商户号
//        String SIGNKEY = ConstantUtil.PARTNER_KEY;//商户密钥

		String APPID = "wx23d5211634ee5234";//公众号的appid
		String MERID = "1454780102";//商户号
		String SIGNKEY = "YBy4MItTte3APraHjMgG5DO8eqEd5nDX";//商户密钥
		String spbill_create_ip = getIpAddr(request);//生产
		System.out.println("spbill_create_ip="+spbill_create_ip);
		//String spbill_create_ip = "";//测试地址，也就是本地真实ip，用于本地测试用
		String scene_info = "{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"www.szyypx.com\",\"wap_name\": \"信息认证\"}}";//我这里是网页入口，app入口参考文档的安卓和ios写法
		String tradeType = "MWEB";//H5支付标记
		//金额转化为分为单位 微信支付以分为单位
		String finalmoney = com.swim.common.utils.StringUtils.getMoney(total_amount);
		//随机数
		String nonce_str= MD5Utils.getMessageDigest(String.valueOf(new Random().nextInt(10000)).getBytes());
//	    //签名数据
//	    StringBuilder sb = new StringBuilder();
//	    sb.append("appid="+APPID);
//	    sb.append("&body="+subject);
//	    sb.append("&mch_id="+MERID);
//	    sb.append("&nonce_str="+nonce_str);
//	    sb.append("&notify_url="+"http://www.szyypx.com/mobile/index.html#/pages/flyshark/my");//这里写回调地址
//	    sb.append("&out_trade_no="+out_trade_no);
//	    sb.append("&scene_info="+scene_info);
//	    sb.append("&sign_type="+"MD5");
//	    sb.append("&spbill_create_ip="+spbill_create_ip);
//	    sb.append("&total_fee="+finalmoney);
//	    sb.append("&trade_type="+tradeType);
//	    sb.append("&key="+SIGNKEY);
//	    System.out.println("sb="+sb);

		//签名数据
		StringBuilder sb = new StringBuilder();
		sb.append("appid="+APPID);
		sb.append("&body="+subject);
		sb.append("&mch_id="+MERID);
		sb.append("&nonce_str="+nonce_str);
		sb.append("&notify_url="+"http://www.szyypx.com/swim/api/pay/notify");
		sb.append("&out_trade_no="+out_trade_no);
		sb.append("&scene_info="+scene_info);
		sb.append("&sign_type="+"MD5");
		sb.append("&spbill_create_ip="+spbill_create_ip);
		sb.append("&total_fee="+finalmoney);
		sb.append("&trade_type="+tradeType);
		sb.append("&key="+SIGNKEY);

		//签名MD5加密  把sb.toString()做MD5操作并且toUpperCase()一下,至于怎么MD5,百度一下或者看官方文档";
		String sign = WXPayUtil.MD5(sb.toString()).toUpperCase();
		System.out.println("sign="+sign);
		System.out.println("签名数据:"+sign);
		//封装xml报文
		String xml="<xml>"+//
				"<appid>"+ APPID+"</appid>"+//
				"<mch_id>"+ MERID+"</mch_id>"+//
				"<nonce_str>"+nonce_str+"</nonce_str>"+//
				"<sign>"+sign+"</sign>"+//
				"<body>"+subject+"</body>"+//
				"<out_trade_no>"+out_trade_no+"</out_trade_no>"+//
				"<total_fee>"+finalmoney+"</total_fee>"+//
				"<trade_type>"+tradeType+"</trade_type>"+//
//	            "<notify_url>"+"http://www.szyypx.com/swim/api/pay/notify"+"</notify_url>"+//这里写回调地址(通知地址)
//	            "<notify_url>"+ URLEncoder.encode("http://www.szyypx.com", "utf-8") +"</notify_url>"+//这里写回调地址(通知地址)
				"<notify_url>"+"http://www.szyypx.com/swim/api/pay/notify"+"</notify_url>"+
				"<sign_type>MD5</sign_type>"+//
				"<scene_info>"+scene_info+"</scene_info>"+//
				"<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"+//
				"</xml>";

		System.out.println("########################");
		System.out.println(xml);
		System.out.println("########################");

		String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信统一下单接口
		String mweb_url = "";
		Map map = new HashMap();
		try {
			//预下单 获取接口地址
			map = getMwebUrl(createOrderURL, xml);
			String return_code  = (String) map.get("return_code");
			String return_msg = (String) map.get("return_msg");
			if("SUCCESS".equals(return_code) && "OK".equals(return_msg)){
				mweb_url = (String) map.get("mweb_url");//调微信支付接口地址
				System.out.println("mweb_url="+mweb_url);
			}else{
				System.out.println("统一支付接口获取预支付订单出错");
				result.put("msg", "支付错误");
				return result.toString();
			}
		} catch (Exception e) {
			System.out.println("统一支付接口获取预支付订单出错");
			e.printStackTrace();
			result.put("msg", "支付错误");
			return result.toString();
		}
		//正常流程用户支付完成后会返回至发起支付的页面，如需返回至指定页面，则可以在MWEB_URL后拼接上redirect_url参数
		result.put("mwebUrl",mweb_url + "&redirect_url="+ URLEncoder.encode("https://www.szyypx.com/mobile/#/pages/order/index?id=" + out_trade_no, "utf-8"));
		//添加微信支付记录日志等操作
		result.put("msg", "success");
		return result.toString();
	}

	/**
	 * 获取用户实际ip
	 * @param request
	 * @return
	 */
	public String getIpAddr(HttpServletRequest request){
		String ipAddress = request.getHeader("x-forwarded-for");
		if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getRemoteAddr();
			if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
				//根据网卡取本机配置的IP
				InetAddress inet=null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ipAddress= inet.getHostAddress();
			}
		}
		//对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
		if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
			if(ipAddress.indexOf(",")>0){
				ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
			}
		}
		return ipAddress;
	}

	/**
	 * 回调接口 （接收通知）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/notify")
	public void weixinPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		BufferedReader reader = request.getReader();
		String line = "";
		Map map = new HashMap();
		String xml = "<xml><return_code><![CDATA[FAIL]]></xml>";;
		JSONObject dataInfo = new JSONObject();
		StringBuffer inputString = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			inputString.append(line);
		}
		request.getReader().close();
		System.out.println("----接收到的报文---"+inputString.toString());
		if(inputString.toString().length()>0){
			map = XMLUtils.parseXmlToList(inputString.toString());
		}else{
			System.out.println("接受微信报文为空");
		}
		System.out.println("map="+map);
		if(map!=null && "SUCCESS".equals(map.get("result_code"))){
			//成功的业务。。。
			String outTradeNo = (String) map.get("out_trade_no");//商户订单号,64个字符以内、可包含字母、数字、下划线；需保证在商户端不重复
			System.out.println("----微信支付成功通知---outTradeNo =" + outTradeNo);

			NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
			natatoriumOrder.setOrderNum(outTradeNo);
			List<NatatoriumOrder> natatoriumOrder1 = natatoriumOrderService.findList(natatoriumOrder);
			orderPaySuccess(natatoriumOrder1.get(0));

			xml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
		}else{
			//失败的业务。。。
			System.out.println("----微信支付失败通知---");
		}
		//告诉微信端已经确认支付成功
		response.getWriter().write(xml);
	}

	/**
	 * 查询订单支付状态
	 * @param outTradeNo
	 * @return
	 */
	@GetMapping("/query/vx/h5/pay/status")
	public String orderStatusQuery(@RequestParam String outTradeNo) {
		NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
		natatoriumOrder.setOrderNum(outTradeNo);
		List<NatatoriumOrder> natatoriumOrderList = natatoriumOrderService.findList(natatoriumOrder);
		if (!CollectionUtils.isEmpty(natatoriumOrderList) && natatoriumOrderList.get(0).getOrderStatus() == 2L) {// 订单状态 2表示已付款
			return "SUCCESS";
		}
		return "TRADE_NOT_EXIST";
	}

	public static Map getMwebUrl(String url,String xmlParam){
		String jsonStr = null;
		HttpClient httpClient = new HttpClient();
		Map map = new HashMap();
		try {
			PostMethod method = null;
			StringRequestEntity reqEntity = new StringRequestEntity(xmlParam,"text/json","UTF-8");
			method = new PostMethod(url);
			method.setRequestEntity(reqEntity);
			method.addRequestHeader("Content-Type","application/json;charset=utf-8");
			httpClient.executeMethod(method);
			StringBuffer resBodyBuf = new StringBuffer();
			byte[] responseBody = new byte[1024];
			int readCount = 0;
			BufferedInputStream is = new BufferedInputStream(method.getResponseBodyAsStream());
			while((readCount = is.read(responseBody,0,responseBody.length))!=-1){
				resBodyBuf.append(new String(responseBody,0,readCount,"utf-8"));
			}
			jsonStr = resBodyBuf.toString();
			System.out.println(jsonStr);
			map = XMLUtils.parseXmlToList(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	//#####################################################################
	//############################# 微信小程序支付  #########################
	//#####################################################################

//	/**
//	 *  根据code获取用户openId、session_key、unionid
//	 * @param code
//	 */
//	public String getOpenIdByCode(String code) {
//		Map<String, String> resultMap = new HashMap<String, String>();
//        //////////////// 1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid ////////////////
//        //授权（必填）
//        String grant_type = "authorization_code";
//        //请求参数
//        String params = "appid=" + "wx2ba65b4022b5220a" + "&secret=" + "c9ebfaa4c1ebb62d2b95319baa633b61" + "&js_code=" + code + "&grant_type=" + grant_type;
//        //发送请求
//        String sr = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
//        //解析相应内容（转换成json对象）
//        Gson gson = new Gson();
//        JsonObject json = gson.fromJson(sr, JsonObject.class);
//        System.out.println(json);
////      JSONObject json = JSONObject.fromObject(sr);
//
//        if (json.get("session_key") == null) {
//        	System.out.println("session_key获取失败");
//        }
//        //获取会话密钥（session_key）
//        String session_key = json.get("session_key").toString().replace("\"", "");
//        //用户的唯一标识（openid）
//        String openid = json.get("openid").toString().replace("\"", "");
//        //用户的唯一标识（openid）
//        String unionid = "";
//        if (StringUtils.isNotEmpty(json.get("unionid") + "")) {
//        	unionid = json.get("unionid").toString().replace("\"", "");
//		}
////        resultMap.put("openid", openid);
////        resultMap.put("unionid", unionid);
////        resultMap.put("session_key", session_key);
//		return openid;
//	}
//
//	/**
//	 * 微信小程序支付
//	 */
//	@RequestMapping(value = "/miniAppPay", method = {RequestMethod.POST, RequestMethod.GET})
//	@ResponseBody
//	public AjaxResult miniAppPay(String code) {
//		//需要通过授权来获取openId
//		String openId = getOpenIdByCode(code);
//		System.out.println("openId="+openId);
//		WxPayApiConfig wxPayApiConfig = WxPayApiConfigKit.getWxPayApiConfig();
//		Map<String, String> params = UnifiedOrderModel
//			.builder()
//			.appid(wxPayApiConfig.getAppId())
//			.mch_id(wxPayApiConfig.getMchId())
//			.nonce_str(WxPayKit.generateStr())
//			.body("IJPay 让支付触手可及-小程序支付")
//			.attach("Node.js 版:https://gitee.com/javen205/TNWXX")
//			.out_trade_no(WxPayKit.generateStr())
//			.total_fee("1000")
//			.spbill_create_ip("47.100.110.4")//服务器公网ip
//			.notify_url("https://www.szyypx.com/swim/api/pay/notify")
//			.trade_type(TradeType.JSAPI.getTradeType())
//			.openid(openId)
//			.build()
//			.createSign(wxPayApiConfig.getPartnerKey(), SignType.HMACSHA256);
//
//		String xmlResult = WxPayApi.pushOrder(false, params);
//        System.out.println("xmlResult="+xmlResult);
//		Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
//
//		String returnCode = result.get("return_code");
//		String returnMsg = result.get("return_msg");
//		if (!WxPayKit.codeIsOk(returnCode)) {
//			return new AjaxResult().addError(returnMsg);
//		}
//		String resultCode = result.get("result_code");
//		if (!WxPayKit.codeIsOk(resultCode)) {
//			return new AjaxResult().addError(returnMsg);
//		}
//		// 以下字段在 return_code 和 result_code 都为 SUCCESS 的时候有返回
//		String prepayId = result.get("prepay_id");
//		Map<String, String> packageParams = WxPayKit.miniAppPrepayIdCreateSign(wxPayApiConfig.getAppId(), prepayId,
//			wxPayApiConfig.getPartnerKey(), SignType.HMACSHA256);
//		String jsonStr = JSON.toJSONString(packageParams);
//        System.out.println("小程序支付的参数:" + jsonStr);
//		return new AjaxResult().success(jsonStr);
//	}


	//#####################################################################
	//############################# 支付宝PC支付  ###########################
	//#####################################################################

	/**
	 * 支付宝网页支付
	 *
	 * @param aliPayBean
	 * @param response
	 */
	@PostMapping("/alipay/webpage")
	public String rechargeAlipayWebPage(@RequestBody AliPayBean aliPayBean, HttpServletResponse response) {
		String paymentRequest = null;
		try {
			response.setContentType("text/html;charset=utf-8");
			paymentRequest = payService.mreadPaymentRequest(aliPayBean);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return paymentRequest;
	}

	/**
	 * 支付宝接口回调
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/aliPay/callback")
	public String notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回数据
        /*
        gmt_create = 2021-01-30 19:46:39
        charset = UTF-8
        gmt_payment = 2021-01-30 19:46:50
        notify_time = 2021-01-30 19:46:51
        subject = 支付价格
        sign = aAfDqNi6U+UO32dV1g2aso
                buyer_id = 2088422
        body = 一笔订单
        invoice_amount = 0.01
        version = 1.0
        notify_id = 2021013000222194650069481
        fund_bill_list = [{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}]
        notify_type = trade_status_sync
        out_trade_no = 20210130074621-417586
        total_amount = 0.01
        trade_status = TRADE_SUCCESS
        trade_no = 2021013022001469481423016991
        auth_app_id = 20210011
        receipt_amount = 0.01
        point_amount = 0.00
        buyer_pay_amount = 0.01
        app_id = 20210011
        sign_type = RSA2
        seller_id = 208893165
        */

		//获取支付宝POST过来反馈信息
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String[]> requestParams = request.getParameterMap();
		for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用
			valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}
		String paramsJson = JSON.toJSONString(params);
		logger.error("支付宝回调，{}", paramsJson);
		try {
			// 调用SDK验证签名
			boolean signVerified = AlipaySignature.rsaCheckV1(params, ConstantUtil.alipay_public_key, ConstantUtil.charset, ConstantUtil.sign_type);
//            if (signVerified) {
			logger.error("支付宝回调签名认证成功=======================");
			// 按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure

			// String passbackParams = params.get("passback_params");//商户传入业务信息，具体值要和支付宝约定，应用于安全，营销等参数直传场景，格式为json格式
			String outTradeNo = params.get("out_trade_no");//商户订单号,64个字符以内、可包含字母、数字、下划线；需保证在商户端不重复
			logger.error("======支付订单号========" + outTradeNo);
			String totalAmount = params.get("buyer_pay_amount");//订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]。
			logger.error("======订单总金额========" + totalAmount);

			if ("TRADE_SUCCESS".equals(params.get("trade_status"))) {
				NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
				natatoriumOrder.setOrderNum(outTradeNo);
				List<NatatoriumOrder> natatoriumOrder1 = natatoriumOrderService.findList(natatoriumOrder);
				orderPaySuccess(natatoriumOrder1.get(0));
			}
			return "success";
//            } else {
//                logger.error("支付宝回调签名认证失败，signVerified=false, paramsJson:{}", paramsJson);
//                return "failure";
//            }
		} catch (AlipayApiException e) {
			logger.error("支付宝回调签名认证失败,paramsJson:{},errorMsg:{}", paramsJson, e.getMessage());
			return "failure";
		}
	}
}
